class LinkedList {
    constructor () {
        this.head = null
        this.length = 0
    }

    // 在链表最后追加元素
    append (ele) {
        let newNode = new LinkedListNode(ele)
        // 判断是否为第一个节点
        if (this.length) {
            let current = this.head
            // 循环找到最后一个节点
            while (current.next) {
                current = current.next
            }
            // 最后一个节点的next指向新创建的节点
            current.next = newNode
        } else {
            this.head = newNode
        }
        this.length++
    }

    insert (position, ele) {
        // 越界判断：1、非负数，2：不得大于链表元素长度
        if (position < 0 || position > this.length) return false

        let newNode = new LinkedListNode(ele)
        let current = this.head
        let previous = current
        let i = 0
        if (position == 0) {
            newNode.next = current
            this.head = newNode
        } else {
            // index !== 0 如果index不等于0，那就遍历找到相应的位置
            // index 0
            while (i++ < position) {
                previous = current
                current = current.next
            }

            newNode.next = current
            previous.next = newNode
        }

        this.length++
        return true
    }

    get (position) {
        // 越界判断：1、非负数，2：不得大于链表元素长度
        if (position < 0 || position >= this.length) return false

        let current = this.head
        let i = 0
        while (i++ < position) {
            current = current.next
        }
        return current.data
    }

    indexOf (data) {
        let current = this.head
        let index = 0
        while (current) {
            if (current.data === data) {
                return index
            }
            current = current.next
            index++
        }
        return -1
    }

    update (index, newData) {
        // 越界判断：1、非负数，2：不得大于链表元素长度
        if (index < 0 || index >= this.length) return false

        let i = 0
        let current = this.head
        while (i++ < index) {
            current = current.next
        }
        current.data = newData
        return true
    }

    removeAt (position) {
        // 越界判断：1、非负数，2：不得大于链表元素长度
        if (position < 0 || position > this.length) return false

        let i = 0
        let current = this.head
        let previous = current

        if (position == 0) {
            this.head = current.next
        } else {
            while (i++ < position) {
                previous = current
                current = current.next
            }
            previous.next = current.next
        }
        this.length--

        return current.data
    }

    remove (data) {
        let position = this.indexOf(data)
        return this.removeAt(position)
    }

    isEmpty () {
        return !this.length
    }

    size () {
        return this.length
    }

    toString () {
        let current = this.head
        let linkedListString = ''
        while (current) {
            linkedListString += current.data + ' '
            current = current.next
        }
        return linkedListString
    }
}

class LinkedListNode {
    constructor (data) {
        this.data = data
        this.next = null
    }
}

export default LinkedList
